package cn.miju.ser.common.rocketmq.consumer;

import cn.miju.alipay.handler.AlipayCloseOrderHandler;
import cn.miju.alipay.request.AlipayCloseOrderReq;
import cn.miju.alipay.response.AlipayBaseResp;
import cn.miju.database.constant.DbConstant;
import cn.miju.database.entity.PayOrder;
import cn.miju.database.service.PayOrderDataService;
import cn.miju.ser.common.rocketmq.Topic;
import cn.miju.ser.framework.handler.MiJuContext;
import cn.miju.ser.framework.handler.MiJuContextHandler;
import cn.miju.unionpay.handler.UnionPayCloseOrderHandler;
import cn.miju.unionpay.request.UnionPayCloseOrderReq;
import cn.miju.unionpay.response.UnionPayBaseResp;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * <pre>
 * 订单超时取消队列
 * </pre>
 *
 * @author zhuming
 * @version 1.0.0
 * @since 2023-03-10 17:45
 */

@Slf4j
@Component
@RequiredArgsConstructor
@RocketMQMessageListener(topic = Topic.TIMEOUT_PAY_ORDER_TOPIC, consumerGroup = "${rocketmq.producer.group}")
public class OrderPayTimeoutListener implements RocketMQListener<Long> {
    private final PayOrderDataService payOrderDataService;
    private final AlipayCloseOrderHandler alipayCloseOrderHandler;
    private final UnionPayCloseOrderHandler unionPayCloseOrderHandler;

    @Override
    public void onMessage(Long payOrderId) {
        PayOrder payOrder = payOrderDataService.getById(payOrderId);
        if (Objects.nonNull(payOrder) && DbConstant.PayOrderStatus.WAITING.equals(payOrder.getStatus())) {
            MiJuContextHandler.setMiJuContext(MiJuContext.builder().merchantId(payOrder.getMerchantId()).build());
            if (DbConstant.PayWay.ALIPAY.equals(payOrder.getPayWay())) {
                log.info("支付宝订单[商户订单号：{}---接入方订单号：{}]超时未支付，执行取消程序", payOrder.getPlatTradeNo(), payOrder.getOutTradeNo());
                AlipayBaseResp baseResp = alipayCloseOrderHandler.doPlan(AlipayCloseOrderReq.builder().
                        outTradeNo(payOrder.getPlatTradeNo()).build());
                if (baseResp.isSuccess() || (DbConstant.PayScen.SCAN_CODE.equals(payOrder.getPayScen()) && baseResp.getCode() == -2)) {
                    payOrder.setStatus(DbConstant.PayOrderStatus.TIMEOUT);
                    payOrder.setUpdatedTime(LocalDateTime.now());
                    payOrderDataService.updateById(payOrder);
                }
            } else if (DbConstant.PayWay.UNIONPAY.equals(payOrder.getPayWay())) {
                log.info("银联（云闪付）订单[商户订单号：{}---接入方订单号：{}]超时未支付，执行取消程序", payOrder.getPlatTradeNo(), payOrder.getOutTradeNo());
                UnionPayBaseResp baseResp = unionPayCloseOrderHandler.doPlan(UnionPayCloseOrderReq.builder().
                        outTradeNo(payOrder.getPlatTradeNo()).build());
                if (baseResp.isSuccess() || (DbConstant.PayScen.SCAN_CODE.equals(payOrder.getPayScen()) && baseResp.getCode() == -3)) {
                    payOrder.setStatus(DbConstant.PayOrderStatus.TIMEOUT);
                    payOrder.setUpdatedTime(LocalDateTime.now());
                    payOrderDataService.updateById(payOrder);
                }
            }
        }
    }
}
